package com.example.expensecontrol;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class FullscreenActivity extends Activity {
	/**
	 * Whether or not the system UI should be auto-hidden after
	 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
	 */
	private static final boolean AUTO_HIDE = true;

	/**
	 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
	 * user interaction before hiding the system UI.
	 */
	private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

	/**
	 * If set, will toggle the system UI visibility upon interaction. Otherwise,
	 * will show the system UI visibility upon interaction.
	 */
	private static final boolean TOGGLE_ON_CLICK = true;

	/**
	 * The flags to pass to {@link SystemUiHider#getInstance}.
	 */
	
	private Spinner expenseType;
	private String expenseTypeVal;
	/**
	 * The instance of the {@link SystemUiHider} for this activity.
	 */
	private static String url = "http://192.168.10.235:8080/macitez001/getJSONResult.action";
	//private static String url = "http://192.168.10.98:8080/testdrive/getJSONResult.action";
	
	private Loadermethod loading;
	private String logstatus;
	ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_fullscreen);

		final View controlsView = findViewById(R.id.fullscreen_content_controls);
		final View contentView = findViewById(R.id.fullscreen_content);
		expenseType=(Spinner)findViewById(R.id.expenseTypeSub);
		
		
		
		List<String> types = new ArrayList<String>();
		types.add("Hotel");
		types.add("Petrol");
		types.add("Auto");
		
		ArrayAdapter<String> typesadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, types);
		typesadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		expenseType.setAdapter(typesadapter);
		expenseType.setSelection(1);
		
		try {
			expenseType.setOnItemSelectedListener(new CustomOnItemSelectedListener());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
	       	 Log.e("Transaction Home",""+e);
		}
		
		pd = new ProgressDialog(this);
		pd.setMessage("Loading...");
		pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	}

	

	
	public class CustomOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			
			 /* Toast.makeText(parent.getContext(), "OnItemSelectedListener : " +
			  parent.getItemAtPosition(pos).toString(),
			  Toast.LENGTH_SHORT).show();*/
			 
			expenseTypeVal=parent.getItemAtPosition(pos).toString();
			
		/********Display Text color in spinner*********/
			
			/*	((TextView) parent.getChildAt(0)).setTextColor(Color.RED);
				((TextView) parent.getChildAt(0)).setTextSize(15);*/
				
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
		}

	}
	
	public void callGetStatusService(View v) {
		
		try {
			
			loading = new Loadermethod();
			loading.execute();	
		} catch (Exception e) {
			// TODO: handle exception
			 e.printStackTrace();
        	 Log.e("GetStatus Activity",""+e);
		}
		
		
		// finish();

	}
	
	public class Loadermethod extends AsyncTask<Void, String, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			String expenseAmt = ((EditText) findViewById(R.id.expenseAmount)).getText().toString();
			String expenseDate = ((DatePicker) findViewById(R.id.expenseDate)).getDayOfMonth()+"";
			JSONParser jParser = new JSONParser();
			// getting JSON string from URL
			JSONObject jsonObject = new JSONObject();	
			try{
				jsonObject.put("expenseAmt", expenseAmt);
				jsonObject.put("expenseType", expenseTypeVal); 
				jsonObject.put("expenseDate", expenseDate); 
				StringEntity se = new StringEntity( jsonObject.toString());  
				JSONObject json = jParser.getJSONFromUrl(url, jsonObject);
				
				logstatus = json.getString("status");
			}catch(Exception e){
				logstatus ="Error :"+e.getMessage();
			}
			
			
			return null;
		}

		
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub

			pd.dismiss();			
			Toast.makeText(getApplicationContext(), "Status : " +
					logstatus,
					  Toast.LENGTH_SHORT).show();
			super.onPostExecute(result);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			pd.show();
			super.onPreExecute();
		}

	}
	
	 /* @Override public void onBackPressed() {
	   // TODO Auto-generated method stub
		 db.close();  
	  }*/
	
}
